import Vue from 'vue';
import VueRouter from 'vue-router';
import { Toast } from 'vant';


Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: { name: 'index' }
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
    },
    {
      name: 'articleDetail',
      path: '/articleDetail/:id',
      component: () => import('@/views/articleDetail.vue')
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/personal.vue')
    },
    {
      name: 'comment',
      path: '/comment/:id',
      component: () => import('@/views/commentlist.vue')
    },
    {
      name: 'myfollows',
      path: '/myfollows',
      component: () => import('@/views/myfollows.vue')
    },
    {
      name: 'mystars',
      path: '/mystars',
      component: () => import('@/views/mystars.vue')
    },
    {
      name: 'edit_profile',
      path: '/edit_profile/:id',
      component: () => import('@/views/edit_profile.vue')
    },
    {
      name: 'cateManager',
      path: '/cateManager',
      component: () => import('@/views/cateManager.vue')
    },
    {
      name: 'Search',
      path: '/search',
      component: () => import('@/views/search.vue')
    }
  ]
});
router.beforeEach((to, from, next) => {
  if (to.name == 'personal' || to.name == 'edit_profile') {
    if (localStorage.getItem('hm_toutiao')) {
      let token = localStorage.getItem('hm_toutiao');
      if (token) {
        next();
      } else {
        Toast('请先登录');
        next({ name: 'login' });
      }
    } else {
      Toast('请先登录');
      next({ name: 'login' });
    }
  } else {
    next();
  }
});
// router.beforeEach((to, from, next) => {
//   // 并不是每个页面的访问都需要先登陆
//   if (to.name == 'personal') { // 这里说明你要访问个人中心页
//     // 如果用户登陆过，则进行路由的跳转，否则重定向到登陆页
//     let token = localStorage.getItem('toutiao_52_token');
//     if (token) { // 如果有token说明你登陆过了
//       next();
//     } else {
//       // 如果是访问需要授权的页面且没有登陆过，则重定向到登陆页
//       next({ name: 'Login' });
//     }
//   } else { // 如果不是访问需要授权的页面，如新闻页，那么不需要判断是否登陆
//     next();
//   }
// });

export default router;